package com.kelvem.saas.workbanch.core.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static boolean isNotEmpty(String str) {
        if (str == null) {
            return false;
        }
        if (str.trim().length() <= 0) {
            return false;
        }
        return true;
    }

    public static boolean isEmpty(String str) {
        if (str == null) {
            return true;
        }
        if (str.trim().length() <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否是有效的数字，包括整数和小数
     *
     * @param str 要判断的字符串
     * @return 如果字符串是有效的数字则返回true，否则返回false
     */
    public static boolean isNumeric(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        // 正则表达式解释：
        // -?         : 可选的负号
        // \\d+       : 一个或多个数字
        // (\\.\\d+)? : 可选的小数部分（包括一个小数点和一个或多个数字）
        String regex = "-?\\d+(\\.\\d+)?";
        return str.matches(regex);
    }
    
    public static String toHTMLString(String in) {
        if (in == null || in.length() <= 0) {
            return "";
        }
        
        StringBuffer out = new StringBuffer();
        for (int i = 0; i < in.length(); i++) {
            char c = in.charAt(i);
            if (c == '\'')
                out.append("'");
            else if (c == '\"')
                out.append("");
            else if (c == '<')
                out.append("&lt;");
            else if (c == '>')
                out.append("&gt;");
            else if (c == '&')
                out.append("&amp;");
            else if (c == ' ')
                out.append("&nbsp;");
            else if (c == '\n')
                out.append("<br>");
            else if (c == '\r')
                continue;
            else
                out.append(c);
        }
        return out.toString();
    }
    
    /**
     * List通过split拼接成 字符串
     *
     * @param list
     * @param delimiter
     * @return String
     * @throws
     */
    public static String toString(List<String> list, String delimiter) {
        
        if (list == null || delimiter == null) {
            return "";
        }
        String result = String.join(delimiter, list.toArray(new String[0]));
        return result;
    }
    
    /**
     * Array通过split拼接成 字符串
     *
     * @param array
     * @param delimiter
     * @return String
     * @throws
     */
    public static String toString(String[] array, String delimiter) {
        
        if (array == null || delimiter == null) {
            return "";
        }
        String result = String.join(delimiter, array);
        return result;
    }
    
    /**
     * trim
     *
     * @param str
     * @return String
     * @throws
     */
    public static String trim(String str) {
        
        if (str == null) {
            return null;
        } else {
            return str.trim();
        }
    }
    
    public static String formatDecimal(BigDecimal decimal, int scale){
        if(decimal==null){
            return "null";
        }
        StringBuilder sb=new StringBuilder("0");
        if(scale>0){
            sb.append(".");
        }
        for(int i=0;i<scale;i++){
            sb.append("0");
        }
        DecimalFormat df = new DecimalFormat(sb.toString());
        df.setRoundingMode(RoundingMode.FLOOR);
        return df.format(decimal);
    }
    
    public static String simpleFormatString(Object str) {
        if (str == null) {
            return null;
        } else {
            return str.toString().toLowerCase().trim();
        }
    }



    private static Pattern aaaAaaPattern = Pattern.compile("[A-Z]");
    private static Pattern aaa_aaaPattern = Pattern.compile("_(\\w)");

    /**
     * <p>aaa_aaa -> aaaAaa</p>
     *
     * @param str
     * @return String
     * @see
     */
    public static String aaa_aaaToaaaAaa(String str) {
        str = str.toLowerCase();
        Matcher matcher = aaa_aaaPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * <p>aaaAaa -> aaa_aaa</p>
     *
     * @param str
     * @return String
     * @see
     */
    public static String aaaAaaToaaa_aaa(String str) {
        Matcher matcher = aaaAaaPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


}
